package com.hhhn.wk.main.tab1.notice;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.hhhn.wk.R;
import com.hhhn.wk.base.BaseActivity;
import com.hhhn.wk.config.Config;
import com.hhhn.wk.main.tab4.allorder.activity.StarSendGoosActivity;
import com.hhhn.wk.main.tab4.allorder.adapter.ParentAdapter;
import com.hhhn.wk.main.tab4.allorder.entity.ItemGoodsBean;
import com.hhhn.wk.main.tab4.allorder.entity.MyOrder;
import com.hhhn.wk.utils.AllPublic;
import com.hhhn.wk.utils.Constants;
import com.hhhn.wk.utils.ToastUtils;
import com.hhhn.wk.widget.adapter.MyAdaper;
import com.hhhn.wk.widget.adapter.ViewHolder;
import com.hhhn.wk.widget.wkpulltorefreshlayout.BaseRefreshListener;
import com.hhhn.wk.widget.wkpulltorefreshlayout.PullToRefreshLayout;
import com.hhhn.wk.widget.wkpulltorefreshlayout.ViewStatus;
import com.hhhn.wk.widget.wkpulltorefreshlayout.util.DisplayUtil;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class NoticeListActivity extends BaseActivity {
    private TextView tv_title;
    /*listView*/
    private ListView mLoadListView;
    private PullToRefreshLayout mPullToRefreshLayout;
    public  ArrayList<Notice> mArrayList = new ArrayList<Notice>();
    private MyAdaper<Notice> myAdaper;


    private int page = 1;
    private int refresh = 0;//0 第一次进入  1下拉刷新  2上啦加载
    private boolean isWU = false;

    private Context context;
    private BaseActivity activity;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    if (mArrayList.size() == 0) {
                        handler.sendEmptyMessage(2);
                    } else {
                        loadData();
                    }
                    break;
                case 2:
                    mPullToRefreshLayout.showView(ViewStatus.EMPTY_STATUS);////空数据
                    break;
                case 3:
                    mPullToRefreshLayout.showView(ViewStatus.ERROR_STATUS);////网络错误
                    break;
            }
            super.handleMessage(msg);
        }
    };


    @Override
    protected void setOnCreateContentView() {
        setContentView(R.layout.activity_notice_list);
        context = NoticeListActivity.this;
        activity = this;
    }

    @Override
    protected void initView() {
        tv_title=findViewById(R.id.tv_title);
        mLoadListView = findViewById(R.id.mLoadListView);
        mPullToRefreshLayout = findViewById(R.id.activity_list);
        mLoadListView.setDivider(new ColorDrawable(getResources().getColor(R.color.colorxian)));
        mLoadListView.setDividerHeight(DisplayUtil.dp2Px(activity, 1));
    }
    @Override
    protected void initData() {
        tv_title.setText("货牛动态");
        getJSON();
    }
    @Override
    protected void initClick() {
        mPullToRefreshLayout.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                refresh = 1;
                page = 1;
                mArrayList.clear();
                if (myAdaper != null) {
                    myAdaper.notifyDataSetChanged();
                }
                isWU = false;
                getJSON();
            }
            @Override
            public void loadMore() {
                if (isWU) {
                    ToastUtils.showShortToast(context, "暂无更多");
                    mPullToRefreshLayout.showView(ViewStatus.CONTENT_END);//暂无更多
                } else {
                    refresh = 2;
                    page++;
                    getJSON();
                }
            }
        });
        mLoadListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                AllPublic.linkH5(activity, activity,"货牛动态", Constants.NOTICEdetails+mArrayList.get(i).getId());
            }
        });
    }
    //获取公告
    private void getJSON(){
        RequestParams params = AllPublic.getParamsDynamic(activity, "/dynamicList", false);
        params.addParameter("currentPage",page);
        params.addParameter("pageSize",10);
        //请求
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override  //成功
            public void onSuccess(String result) {

                Log.d("##", "获取公告: "+result);
                try {
                    JSONObject jsonObject= new JSONObject(result);
                    String reason = jsonObject.getString("code");
                    if (reason.equals("0")) {
                        JSONArray data = jsonObject.getJSONArray("data");
                        Notice mNotice=null;
                        if (data.length()<10){
                            isWU = false;
                        }
                        for (int i=0;i<data.length();i++){
                            mNotice=new Notice();
                            JSONObject object = data.getJSONObject(i);
                            String id = object.has("id") ? object.getString("id") : "";
                            String title = object.has("title") ? object.getString("title") : "";
                            String cmIndate = object.has("cmIndate") ? object.getString("cmIndate") : "";
                            String content = object.has("remark") ? object.getString("remark") : "";
                            String image_url = object.has("imageUrl") ? object.getString("imageUrl") : "";
                            String web_url = object.has("webUrl") ? object.getString("webUrl") : "";
                            mNotice.setId(id);
                            mNotice.setTitle(title);
                            mNotice.setCmIndate(cmIndate);
                            mNotice.setContent(content);
                            mNotice.setImage_url(image_url);
                            mNotice.setWeb_url(web_url);

                            mArrayList.add(mNotice);
                        }
                        handler.sendEmptyMessage(1);
                    }else {
                        Toast.makeText(activity,jsonObject.getString("errMsg"), Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ex.printStackTrace();
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });
    }
    private void loadData() {
        if (myAdaper==null){
            myAdaper=new MyAdaper<Notice>(mArrayList,R.layout.item_notice_list) {
                @Override
                public void bindView(ViewHolder holder, Notice obj, int position) {
                    holder.setText(R.id.tv_title,obj.getTitle());
                    Log.d("@@", "bindView: "+obj.getTitle());
                    holder.setText(R.id.tv_address,obj.getContent());
                    holder.setText(R.id.tv_time,obj.getCmIndate());
                    holder.setImageyuan(R.id.img,obj.getImage_url());
                }
            };
            mLoadListView.setAdapter(myAdaper);
        }else {
            myAdaper.notifyDataSetChanged();
        }
        if (refresh == 1) {
            mPullToRefreshLayout.finishRefresh();//下拉刷新
        } else if (refresh == 2) {
            mPullToRefreshLayout.finishLoadMore();//上啦加载
        }
        mPullToRefreshLayout.showView(ViewStatus.CONTENT_STATUS);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mArrayList != null) {
            mArrayList.clear();
        }
        if (myAdaper != null) {
            myAdaper = null;
        }
    }
}
